<template>
    <a-cascader :options=$store.state.rank.collage_list :load-data="loadData" :default-value="['1','3','9174848677']" change-on-select size="large" placeholder="学院/专业/班级"  @change="onCascaderChange" />
</template>
<script>
    export default {
        data(){
            return({
                majorError: false,
                classError: false
            })
        },
        methods: {
            onCascaderChange(value) {
                this.$store.state.rank.rankData.colId = value[0];
                this.$store.state.rank.rankData.majorId = value[1];
                this.$store.state.rank.rankData.classId = value[2];

            },
            loadData(selectedOptions) {
                const targetOption = selectedOptions[selectedOptions.length - 1];
                targetOption.loading = true;
            },

        },
        mounted() { //初始获取学院和专业和班级
            this.$store.state.rank.rankData.colId = '1';
            this.$store.state.rank.rankData.majorId = '3';
            this.$store.state.rank.rankData.classId = '9174848677';
            this.$store.dispatch('rank/getCollage').then(//获取学院
                res=>{
                    if(res.code === 0){
                        this.$message.error("存在"+res.msg);
                    } else if(res.code===1){
                        //获取专业
                        this.$store.state.rank.collage_list.forEach(item=>{
                            this.$store.dispatch('rank/getMajor',item.value).then(
                                majRes=>{
                                    if(this.majorError===false) {
                                        if(majRes==null){
                                            this.$message.error('网络异常');
                                        }
                                        else if(majRes.code === 0){
                                                this.$message.error("存在"+majRes.msg);
                                            }
                                        this.majorError = true;
                                    }else if (majRes.code === 1){
                                        const colList = this.$store.state.rank.collage_list;
                                        colList.forEach(item=>{
                                            this.$store.dispatch('rank/getClass',item.value).then(
                                                claRes =>{
                                                    if(this.classError===false) {
                                                        if (claRes == null) {
                                                            this.$message.error('网络异常');
                                                        } else if (claRes.code === 0) {
                                                            this.$message.error("存在"+claRes.msg);
                                                        }
                                                        this.classError = true;
                                                    }
                                                }
                                            ).catch(()=>{
                                                this.$message.error("网络异常")
                                            })
                                        })
                                    }
                                }
                            ).catch(()=>{
                                this.$message.error("网络异常")
                            })
                        })
                    }
                }
            ).catch(()=>{
                this.$message.error("网络异常")
            })
        }
    };
</script>
